/**
 * Copyright 2008 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kiff.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import edu.emory.mathcs.backport.java.util.Collections;

/**
 * Default Implementation of the {@link kiff.util.Loader} interface.
 * @author Adam
 * @version $Id: DefaultLoader.java 62 2008-10-31 04:51:50Z a.ruggles $
 * 
 * Created on Oct 12, 2008 at 1:01:55 PM 
 */
@SuppressWarnings("unchecked")
public class DefaultLoader implements Loader {
	/**
	 * A Map to store the settings.
	 */
	private final Map<String, Object> settings;

	/**
	 * Constructs a Loader Object.
	 */
	public DefaultLoader() {
		settings = Collections.synchronizedMap(new HashMap<String, Object>());
		Properties properties = PropertiesUtils.load(DEFAULT_SETTINGS);
		for (Map.Entry<Object, Object> entry : properties.entrySet()) {
			settings.put(String.valueOf(entry.getKey()), entry.getValue());
		}
	}

	/**
	 * Constructs a Loader Object.
	 * @param file The settings file.
	 */
	public DefaultLoader(final String file) {
		settings = Collections.synchronizedMap(new HashMap<String, Object>());
		Properties properties = PropertiesUtils.load(file);
		for (Map.Entry<Object, Object> entry : properties.entrySet()) {
			settings.put(String.valueOf(entry.getKey()), entry.getValue());
		}
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.Loader#getAsProperties()
	 */
	public synchronized Properties getAsProperties() {
		Properties props = new Properties();
		for (Map.Entry<String, Object> entry : settings.entrySet()) {
			props.put(entry.getKey(), entry.getValue());
		}
		return props;
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.Loader#getSetting(java.lang.Class, kiff.util.LoaderSetting)
	 */
	public <T> T getSetting(final Class<T> valueClass, final LoaderSetting loaderSetting) {
		return valueClass.cast(settings.get(loaderSetting.toString()));
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.Loader#getSetting(java.lang.Class, java.lang.String)
	 */
	public <T> T getSetting(final Class<T> valueClass, final String name) {
		return valueClass.cast(settings.get(name));
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.Loader#getSetting(kiff.util.LoaderSetting)
	 */
	public Object getSetting(final LoaderSetting loaderSetting) {
		return settings.get(loaderSetting.toString());
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.Loader#getSetting(java.lang.String)
	 */
	public Object getSetting(final String name) {
		return settings.get(name);
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.Loader#setSetting(kiff.util.LoaderSetting, java.lang.Class, java.lang.Object)
	 */
	public <T> void setSetting(final LoaderSetting loaderSetting, final Class<T> valueClass, final T value) {
		settings.put(loaderSetting.toString(), value);
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.Loader#setSetting(kiff.util.LoaderSetting, java.lang.Object)
	 */
	public void setSetting(final LoaderSetting loaderSetting, final Object value) {
		settings.put(loaderSetting.toString(), value);
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.Loader#setSetting(java.lang.String, java.lang.Class, java.lang.Object)
	 */
	public <T> void setSetting(final String name, final Class<T> valueClass, final T value) {
		settings.put(name, value);
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.Loader#setSetting(java.lang.String, java.lang.Object)
	 */
	public void setSetting(final String name, final Object value) {
		settings.put(name, value);
	}
}
